package com.quzhi.sys.controller.receiveGoods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.Result;
import com.quzhi.common.code.Code;
import com.quzhi.sys.entity.GoodsSign;
import com.quzhi.sys.entity.OrderRemark;
import com.quzhi.sys.service.impl.GoodsSignServiceImpl;
import com.quzhi.sys.service.impl.OrderRemarkServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Xibing
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/sys/goodsSign")
public class GoodsSignController {
    @Autowired
    private GoodsSignServiceImpl goodsSignService;


    @GetMapping("/list")
    public Result<?> getRemarks() {
        // 删除今天之前已处理的登记信息
        List<GoodsSign> list = goodsSignService.list();

        LocalDate now = LocalDate.now();
        for (GoodsSign goodsSign : list) {
            if (now.isAfter(goodsSign.getCreateTime().toLocalDate())) {
                if (goodsSign.getStatus()) {
                    goodsSignService.removeById(goodsSign);
                }
            }
        }

        return Result.success(goodsSignService.list());
    }


    @GetMapping("/{id}")
    public Result<?> getRemarkById(@PathVariable("id") String id) {
        GoodsSign byId = goodsSignService.getById(id);

        return Result.success(byId);
    }

    /**
     * 录入快递单号后检测该单号是否有登记记录
     *
     * @return
     */
    @GetMapping("/isSign")
    public Result<?> getIsSignFlag(@RequestParam String oodNum) {
        // 根据运单号查询登记记录,未处理
        LambdaQueryWrapper<GoodsSign> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsSign::getStatus, Code.WEI_CHU_LI);
        wrapper.eq(GoodsSign::getNumber, oodNum);
        GoodsSign one = goodsSignService.getOne(wrapper);

        if (one == null) return Result.fail(Code.UPDATE_FAIL_CODE, "未登记过");


        return Result.success(one, "该运单号已登记，请处理。");
    }

    // 添加备注
    @PostMapping
    public Result<?> addRemark(@RequestBody GoodsSign goodsSign) {

        // 判断该单号是否已添加
        String number = goodsSign.getNumber();

        LambdaQueryWrapper<GoodsSign> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsSign::getNumber, number);
        wrapper.eq(GoodsSign::getStatus, Code.WEI_CHU_LI);

        GoodsSign one = goodsSignService.getOne(wrapper);

        if (one != null) return Result.fail(Code.UPDATE_FAIL_CODE, "该运单号已登记");

        return goodsSignService.addGoodsSignService(goodsSign);

    }

    // 更新备注
    @PutMapping
    public Result<?> updateRemark(@RequestBody GoodsSign goodsSign) {

        return goodsSignService.updateGoodsSignService(goodsSign);
    }

    // 更新登记备注状态(如果是已处理，则更新成未处理，反之相反)
    @PutMapping("/updateStatus/{id}")
    public Result<?> updateStatus(@PathVariable("id") String id) {
        // 获取登记信息
        GoodsSign byId = goodsSignService.getById(id);

        Boolean status = byId.getStatus();
        status = !status;
        byId.setStatus(status);

        goodsSignService.updateById(byId);

        return Result.success("状态更新成功");
    }

    // 删除备注
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable("id") String id) {
        goodsSignService.removeById(id);
        return Result.success("删除成功");
    }

}
